package com.wzx.service;

import com.wzx.core.ioc.anootaction.Autowired;
import com.wzx.core.ioc.anootaction.Service;
import com.wzx.core.ioc.anootaction.Transactional;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


@Service
public class TransferService {

    @Autowired
    private Connection connection;

    /**
     *
     * @param userId1 付款人
     * @param userId2 收款人
     * @param money 金额
     */
    @Transactional
    public void transfer(Integer userId1, Integer userId2, Integer money) throws SQLException {
        changeUserMoney(userId1, -money);
//        System.out.println(2 / 0);
        changeUserMoney(userId2, +money);
        System.out.println("转账完成");
    }

    private void changeUserMoney(Integer userId, Integer money) throws SQLException {
        PreparedStatement ps = connection.prepareStatement("update t_user set money = money + ? where id=?");
        ps.setInt(1, money);
        ps.setInt(2, userId);
        ps.executeUpdate();
    }
}
